package matchthreegame.handlers;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import matchthreegame.dataBase.DBManagerHelper;
import matchthreegame.extensions.MatchThreeGameZoneExtension;

import com.smartfoxserver.v2.buddylist.Buddy;
import com.smartfoxserver.v2.buddylist.BuddyList;
import com.smartfoxserver.v2.buddylist.BuddyListManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class GetRankingListsHandler extends BaseClientRequestHandler {

	private final String FRIENDS_RANKING_LIST = "FRL";
	private final String WORLD_RANKING_LIST   = "WRL";
	
	MatchThreeGameZoneExtension zoneExtension;
	
	@Override
	public void handleClientRequest(User sender, ISFSObject arg1) {
		
		zoneExtension = (MatchThreeGameZoneExtension)getParentExtension().getParentZone().getExtension();
				
		// NOT USED
//		ArrayList<String> friendNames = getFriendNames(sender);
		
		WeakReference<DBManagerHelper> dbHelper = null;
		ArrayList<HashMap<String, String>> worldRanking = new ArrayList<HashMap<String, String>>();
		ArrayList<HashMap<String, String>> friendsRanking = new ArrayList<HashMap<String, String>>();
		
		try {
			dbHelper = new WeakReference<DBManagerHelper>(zoneExtension.getDBHelper());
			
			// get array with HashMap containing NAME and WINS params
			worldRanking = dbHelper.get().getWorldRankingList(sender);
			
			// NOT USED
//			friendsRanking = dbHelper.get().getFriendsRankingList(friendNames);
			
		} catch (Exception e) {
			trace(e.getMessage().toString());
		}
		
		zoneExtension.trace(sender.getName() + " seen Leaderboard");
//		zoneExtension.trace("worldRanking: " + worldRanking);
//		zoneExtension.trace("friendsRanking: " + friendsRanking);
		
		SFSArray sfsWorldRankingList = convertArrayListToSFSArray(worldRanking);
		SFSArray sfsFriendsRankingList = convertArrayListToSFSArray(friendsRanking);
		
		// Send the two SFSArrays in SFSObject with the Ranking Lists back to the sender
		SFSObject sfsParams = new SFSObject();
		sfsParams.putSFSArray(WORLD_RANKING_LIST,sfsWorldRankingList);
		sfsParams.putSFSArray(FRIENDS_RANKING_LIST,sfsFriendsRankingList);
		
		zoneExtension.send(MatchThreeGameZoneExtension.CMD_GET_RANKING_LISTS, sfsParams, sender);
		
	}
	
	
	private SFSArray convertArrayListToSFSArray(ArrayList<HashMap<String, String>> rankingList)
	{
		
		SFSArray sfsRankingList = new SFSArray();
		
		// Put all user info's in SFSArray with SFSObjects
		for (int i = 0; i < rankingList.size(); i++) {
			
			HashMap<String, String> userInfo = rankingList.get(i);
			
			SFSObject sfsUserInfo = new SFSObject();
			sfsUserInfo.putUtfString(DBManagerHelper.NAME, userInfo.get(DBManagerHelper.NAME));
			sfsUserInfo.putUtfString(DBManagerHelper.RANK, userInfo.get(DBManagerHelper.RANK));
			sfsUserInfo.putUtfString(DBManagerHelper.WIN_PERCENTAGE, userInfo.get(DBManagerHelper.WIN_PERCENTAGE));
			sfsUserInfo.putUtfString(DBManagerHelper.PLAYED_GAMES, userInfo.get(DBManagerHelper.PLAYED_GAMES));
			
			sfsRankingList.addSFSObject(sfsUserInfo);
		}
		
		return sfsRankingList;
	}
	
	
	private ArrayList<String> getFriendNames(User user)
	{
						
		// get BuddyList of user
		BuddyListManager buddyListManager = zoneExtension.getParentZone().getBuddyListManager();
		BuddyList buddyList = buddyListManager.getBuddyList(user.getName());
		List<Buddy> buddies = buddyList.getBuddies();
		
		ArrayList<String> friendNames = new ArrayList<String>();
				
		// Add all buddy names in Array with strings
		for (Buddy buddy : buddies) {
			friendNames.add(buddy.getName());
		}
				
		return friendNames;
		
	}

}
